package com.cangchu.ui.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import butterknife.BindView;
import butterknife.OnClick;
import com.cangchu.R;
import com.cangchu.adapter.GoodsInfoAdapter;
import com.cangchu.db.domain.Batch;
import com.cangchu.db.domain.Goods;
import com.cangchu.presenter.GoodsInfoPresenter;
import com.cangchu.presenter.GoodsInfoPresenterImpl;
import com.cangchu.view.GoodsInfoView;

/**
 * 商品信息扫描类，要求根据扫描的数据可以获取商品信息，并且列出相应的批次供使用者选择
 * 使用者可以选择填写盘点的数量，然后点击提交数据。提交的数据直接上传Bomb云端？
 * 使用MVP模式
 * Created by 张博文 on 2016/11/8.
 */
public class GoodsInfoFragment extends newBaseFragment implements GoodsInfoView {


    @BindView(R.id.fgi_code)
    EditText fgiCode;
    @BindView(R.id.fgi_count)
    EditText fgiCount;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.fgi_info)
    EditText fgiInfo;
    @BindView(R.id.fgi_rv)
    RecyclerView fgiRv;

    private GoodsInfoPresenter presenter;
    private GoodsInfoAdapter adapter;


    @OnClick(R.id.button)
    public void onClick(View view) {
        submitInfo();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_goodsinfo;
    }

    @Override
    protected void init() {
        super.init();

        presenter = new GoodsInfoPresenterImpl(this);
        fgiRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        fgiRv.setHasFixedSize(true);
        fgiRv.setAdapter(adapter);
        fgiCode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.d(TAG, String.valueOf(hasFocus));
                presenter.getGoodsInfo(fgiCode.getText().toString());
            }
        });
    }

    //根据获取的商品，还要能够修改RecycleView。
    @Override
    public void CodeChange(Goods goods) {
        if (goods != null) {
            fgiInfo.setText(goods.toString());
        } else {
            fgiInfo.setText("商品信息未找到");
        }
    }

    @Override
    public void updateBatchInfo(Goods goods) {

    }

    @Override
    public void submitInfo() {

        Log.d(TAG, "提交数据");
        System.out.println(fgiCount.getText());

    }

    @Override
    public void selectBatch(Batch batch) {
        fgiInfo.append(batch.toString());
    }

}
